package org.rsbot.script.methods;

import org.rsbot.client.Client;
import org.rsbot.client.RSInterface;

class GameGUI extends MethodProvider
{
  private int ind_GUI;
  private int ind_Minimap;
  private int ind_Compass;
  private int[] ind_Tabs;

  public GameGUI(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
    resetIDs();
  }

  private synchronized void checkGUI()
  {
    if (this.ind_GUI != this.methods.client.getGUIRSInterfaceIndex()) {
      resetIDs();
      this.ind_GUI = this.methods.client.getGUIRSInterfaceIndex();
    }
  }

  public synchronized RSInterface getCompass()
  {
    checkGUI();

    Object localObject = this.ind_GUI != -1 ? this.methods.client.getRSInterfaceCache()[this.ind_GUI] : null;
    if (localObject == null) {
      return null;
    }

    if (this.ind_Compass == -1) {
      for (int i = 0; i < localObject.length; i++) {
        if ((localObject[i] != null) && (localObject[i].getActions() != null) && (localObject[i].getActions().length == 1) && (localObject[i].getActions()[0].equals("Face North"))) {
          this.ind_Compass = i;
          break;
        }
      }

    }

    if (this.ind_Compass != -1) {
      return localObject[this.ind_Compass];
    }
    return null;
  }

  public synchronized RSInterface getMinimapInterface()
  {
    checkGUI();

    Object localObject = this.ind_GUI != -1 ? this.methods.client.getRSInterfaceCache()[this.ind_GUI] : null;
    if (localObject == null) {
      return null;
    }

    if (this.ind_Minimap == -1) {
      for (int i = 0; i < localObject.length; i++) {
        if ((localObject[i] != null) && (localObject[i].getSpecialType() == 1338)) {
          this.ind_Minimap = i;
          break;
        }
      }

    }

    if (this.ind_Minimap != -1) {
      return localObject[this.ind_Minimap];
    }
    return null;
  }

  public synchronized RSInterface getTab(int paramInt)
  {
    if ((paramInt < 0) || (paramInt >= this.ind_Tabs.length)) {
      return null;
    }

    checkGUI();

    Object localObject = this.ind_GUI != -1 ? this.methods.client.getRSInterfaceCache()[this.ind_GUI] : null;
    if (localObject == null) {
      return null;
    }

    if (this.ind_Tabs[paramInt] == -1) {
      for (int i = 0; i < localObject.length; i++) {
        if ((localObject[i] != null) && (localObject[i].getActions() != null) && (localObject[i].getActions().length > 0) && (localObject[i].getActions()[0].equals(Game.TAB_NAMES[paramInt]))) {
          this.ind_Tabs[paramInt] = i;
          break;
        }
      }

    }

    if (this.ind_Tabs[paramInt] != -1) {
      return localObject[this.ind_Tabs[paramInt]];
    }
    return null;
  }

  private synchronized void resetIDs()
  {
    this.ind_GUI = -1;
    this.ind_Minimap = -1;
    this.ind_Compass = -1;

    this.ind_Tabs = new int[17];
    for (int i = 0; i < this.ind_Tabs.length; i++)
      this.ind_Tabs[i] = -1;
  }
}